﻿(function(a) {
	a.PaginationCalculator = function(b, c) {
		this.maxentries = b;
		this.opts = c
	};
	a.extend(a.PaginationCalculator.prototype, {
		numPages: function() {
			return Math.ceil(this.maxentries / this.opts.items_per_page)
		},
		getInterval: function(b) {
			var d = Math.floor(this.opts.num_display_entries / 2);
			var e = this.numPages();
			var g = e - this.opts.num_display_entries;
			var f = b > d ? Math.max(Math.min(b - d, g), 0) : 0;
			var c = b > d ? Math.min(b + d + (this.opts.num_display_entries % 2), e) : Math.min(this.opts.num_display_entries, e);
			return {
				start: f,
				end: c
			}
		}
	});
	a.PaginationRenderers = {};
	a.PaginationRenderers.defaultRenderer = function(b, c) {
		this.maxentries = b;
		this.opts = c;
		this.pc = new a.PaginationCalculator(b, c)
	};
	a.extend(a.PaginationRenderers.defaultRenderer.prototype, {
		createLink: function(f, c, b) {
			var d, e = this.pc.numPages();
			f = f < 0 ? 0 : (f < e ? f : e - 1);
			b = a.extend({
				text: f + 1,
				classes: ""
			}, b || {});
			if(f == c) {
				d = a("<span class='current'>" + b.text + "</span>")
			} else {
				d = a("<a>" + b.text + "</a>").attr("href", this.opts.link_to.replace(/__id__/, f))
			}
			if(b.classes) {
				d.addClass(b.classes)
			}
			d.data("page_id", f);
			return d
		},
		appendRange: function(b, c, g, d, f) {
			var e;
			for(e = g; e < d; e++) {
				this.createLink(e, c, f).appendTo(b)
			}
		},
		getLinks: function(c, e) {
			var b, d, g = this.pc.getInterval(c),
				h = this.pc.numPages(),
				f = a("<div class='pagination'></div>");
			if(this.opts.prev_text && (c > 0 || this.opts.prev_show_always)) {
				f.append(this.createLink(c - 1, c, {
					text: this.opts.prev_text,
					classes: "prev"
				}))
			}
			if(g.start > 0 && this.opts.num_edge_entries > 0) {
				d = Math.min(this.opts.num_edge_entries, g.start);
				this.appendRange(f, c, 0, d, {
					classes: "sp"
				});
				if(this.opts.num_edge_entries < g.start && this.opts.ellipse_text) {
					jQuery("<span>" + this.opts.ellipse_text + "</span>").appendTo(f)
				}
			}
			this.appendRange(f, c, g.start, g.end);
			if(g.end < h && this.opts.num_edge_entries > 0) {
				if(h - this.opts.num_edge_entries > g.end && this.opts.ellipse_text) {
					jQuery("<span>" + this.opts.ellipse_text + "</span>").appendTo(f)
				}
				b = Math.max(h - this.opts.num_edge_entries, g.end);
				this.appendRange(f, c, b, h, {
					classes: "ep"
				})
			}
			if(this.opts.next_text && (c < h - 1 || this.opts.next_show_always)) {
				f.append(this.createLink(c + 1, c, {
					text: this.opts.next_text,
					classes: "next"
				}))
			}
			a("a", f).click(e);
			return f
		}
	});
	a.fn.pagination = function(e, g) {
		g = jQuery.extend({
			items_per_page: 10,
			num_display_entries: 11,
			current_page: 0,
			num_edge_entries: 0,
			link_to: "#",
			prev_text: "Prev",
			next_text: "Next",
			ellipse_text: "...",
			prev_show_always: true,
			next_show_always: true,
			renderer: "defaultRenderer",
			load_first_page: false,
			callback: function() {
				return false
			}
		}, g || {});
		var b = this,
			j, d, c;

		function h(m) {
			var n, o = a(m.target).data("page_id"),
				l = k(o);
			if(!l) {
				m.stopPropagation()
			}
			return l
		}

		function k(m) {
			b.data("current_page", m);
			d = j.getLinks(m, h);
			b.empty();
			d.appendTo(b);
			var l = g.callback(m, b);
			return l
		}
		c = g.current_page;
		b.data("current_page", c);
		e = (!e || e < 0) ? 1 : e;
		g.items_per_page = (!g.items_per_page || g.items_per_page < 0) ? 1 : g.items_per_page;
		if(!a.PaginationRenderers[g.renderer]) {
			throw new ReferenceError("Pagination renderer '" + g.renderer + "' was not found in jQuery.PaginationRenderers object.")
		}
		j = new a.PaginationRenderers[g.renderer](e, g);
		var i = new a.PaginationCalculator(e, g);
		var f = i.numPages();
		b.bind("setPage", {
			numPages: f
		}, function(l, m) {
			if(m >= 0 && m < l.data.numPages) {
				k(m);
				return false
			}
		});
		b.bind("prevPage", function(m) {
			var l = a(this).data("current_page");
			if(l > 0) {
				k(l - 1)
			}
			return false
		});
		b.bind("nextPage", {
			numPages: f
		}, function(m) {
			var l = a(this).data("current_page");
			if(l < m.data.numPages - 1) {
				k(l + 1)
			}
			return false
		});
		d = j.getLinks(c, h);
		b.empty();
		d.appendTo(b);
		if(g.load_first_page) {
			g.callback(c, b)
		}
	}
})(jQuery);
var RemmendList, PageSize = 10,
	productlist = "",
	pagination = $("#pagination"),
	timerId, pageInfo = $("#page-info"),
	pageCount = 0;
var TickSpace = 5000;
var ItemRows = 60;

function GetProductByBrowseThenPurchase(a, b) {
	GetRecommandProduct(a, 0, 1, b)
}

function GetProductByBrowseThenBrowse(a, b) {
	GetRecommandProduct(a, 0, 1, b)
}

function GetProductByPurchaseThenPurchase(a, d, b, c) {
	GetRecommandProduct(a, d, b, c)
}

function GetRecommandProduct(b, d, e, c) {
	var a = {
		Message: "调用成功",
		Success: true,
		Title: "为我推荐",
		Items: [{
			MarketPrice: "349.00",
			Price: "349.00",
			ProductCode: "6358704",
			ProductName: "凡客衬衫 吉国武 免烫 温莎领 2.0 浅蓝条纹",
			ProductUrl: "http://item.vancl.com/6358704.html",
			SmallImage: "6358704-1j201508281548494443.jpg",
			ref: "r-my-odr-1"
		}, {
			MarketPrice: "599.00",
			Price: "299.00",
			ProductCode: "1932949",
			ProductName: "凡客80免烫衬衫 小方领 白色",
			ProductUrl: "http://item.vancl.com/1932949.html",
			SmallImage: "1932949-0201409051758163580.jpg",
			ref: "r-my-odr-2"
		}, {
			MarketPrice: "299.00",
			Price: "168.00",
			ProductCode: "6373840",
			ProductName: "凡客衬衫 牛津纺 短袖 天蓝",
			ProductUrl: "http://item.vancl.com/6373840.html",
			SmallImage: "6373840-1j201605191154496897.jpg",
			ref: "r-my-odr-3"
		}, {
			MarketPrice: "599.00",
			Price: "249.00",
			ProductCode: "6139863",
			ProductName: "凡客麻衬衫裙 蓝色",
			ProductUrl: "http://item.vancl.com/6139863.html",
			SmallImage: "6139863-1j201505141032063416.jpg",
			ref: "r-my-odr-4"
		}, {
			MarketPrice: "299.00",
			Price: "168.00",
			ProductCode: "6373888",
			ProductName: "凡客T恤 水柔棉 短袖圆领 男款 黑色",
			ProductUrl: "http://item.vancl.com/6373888.html",
			SmallImage: "6373888-1j201605091943595236.jpg",
			ref: "r-my-odr-5"
		}, {
			MarketPrice: "268.00",
			Price: "129.00",
			ProductCode: "6189289",
			ProductName: "凡客T恤 supima 男款 黑色",
			ProductUrl: "http://item.vancl.com/6189289.html",
			SmallImage: "6189289-1j201505051713244248.jpg",
			ref: "r-my-odr-6"
		}, {
			MarketPrice: "399.00",
			Price: "168.00",
			ProductCode: "6374366",
			ProductName: "凡客POLO衫 长绒棉 男款 藏蓝色",
			ProductUrl: "http://item.vancl.com/6374366.html",
			SmallImage: "6374366-1j201605061239335782.jpg",
			ref: "r-my-odr-7"
		}, {
			MarketPrice: "1999.00",
			Price: "399.00",
			ProductCode: "6373889",
			ProductName: "凡客T恤 水柔棉jade cotton 短袖圆领 男款 白色",
			ProductUrl: "http://item.vancl.com/6373889.html",
			SmallImage: "6373889-1j201606081613527558.jpg",
			ref: "r-my-odr-8"
		}, {
			MarketPrice: "699.00",
			Price: "299.00",
			ProductCode: "6358703",
			ProductName: "凡客休闲裤 100支 黑色",
			ProductUrl: "http://item.vancl.com/6358703.html",
			SmallImage: "6358703-1j201506291714306524.jpg",
			ref: "r-my-odr-9"
		}, {
			MarketPrice: "368.00",
			Price: "249.00",
			ProductCode: "6374279",
			ProductName: "凡客牛仔裤 轻弹直筒  男款 中蓝色",
			ProductUrl: "http://item.vancl.com/6374279.html",
			SmallImage: "6374279-1j201604211543094550.jpg",
			ref: "r-my-odr-10"
		}]
	};
	$("[fid=titname]").text(a.Title);
	if($("#recommandtype").val() != "order") {
		if($("#product-code-list").val() == "") {
			$("[fid=titname]").text("为您推荐")
		}
	}
	if($("#recommandtype").val() == "order") {
		if($("#product-code-list").val() == "") {
			$("[fid=titname]").text("为您推荐")
		} else {
			$("[fid=titname]").text("购买相同商品的顾客还购买了：")
		}
	}
	RemmendList = a;
	InitRecommendList()
}

function RecommendListLi(c) {
	var a = c * PageSize;
	var b = a + PageSize;
	if(RemmendList.Success) {
		pageInfo.children("label").text(pagination.data("current_page") + 1);
		var d = "";
		$("#recommendlist").fadeOut(500, function() {
			var e = new Array(6);
			e[0] = "first";
			e[1] = "second";
			e[2] = "third";
			e[3] = "fourth";
			e[4] = "fifth";
			e[5] = "sixth";
			e[6] = "seven";
			e[7] = "eight";
			e[8] = "nine";
			e[9] = "ten";
			$.each(RemmendList.Items, function(f, j) {
				if(f >= a && f < b) {
					var n = j.ProductName;
					var k = n.getBytesLength();
					if(k > 42) {
						n = n.intercept(42) + "..."
					}
					j.ProductUrl = "http://item.vancl.com/" + j.ProductCode.toString() + ".html";
					var m = j.ProductCode;
					var o = j.ref;
					var l = "my-recommend-" + e[f % 10] + "-producname";
					var h = "my-recommend-" + e[f % 10] + "-productpicture";
					var g = "http://p1.vanclimg.com/130/product/" + m.substring(0, 1) + "/" + m.substring(1, 2) + "/" + m.substring(2, 3) + "/" + m + "/big/" + j.SmallImage;
					d += "<li><p class='pic'><a href='" + j.ProductUrl + "' rel = '" + o + "' class='track' name='" + h + "' target='_blank' title='" + j.ProductName + "'><img src='" + g + "' width='130' height='130' /></a></p>";
					d += "<p class='prod_name'><a href='" + j.ProductUrl + "' rel = '" + o + "'  class='track' name='" + l + "' target='_blank' title='" + j.ProductName + "'>" + n + "</a></p> ";
					d += "<p class='s_price'>市场价：￥<span>" + j.MarketPrice + "</span></p><p class='price'>售价：￥" + j.Price + "</p></li>"
				}
				productlist += j.ProductCode + ","
			});
			$("#recommendlist").html(d);
			$("#recommendlist").fadeIn(500)
		});
		$("#pagination").find(".prev").attr("class", "prev track").attr("name", "my-recommend-previous page");
		$("#pagination").find(".next").attr("class", "next track").attr("name", "my-recommend-next page");
		return false
	}
}

function setTimer(c, d, b) {
	var a = arguments;
	timerId = setTimeout(function() {
		b = b || [];
		c.apply(this, b);
		a.callee(c, d, b)
	}, d)
}

function autoClick() {
	if(parseInt(pageCount) && pageCount < 2) {
		return
	}
	var a = pagination.find(".next"),
		b = pagination.find(".prev");
	if(a.data("page_id") === pagination.data("current_page")) {
		b.data("page_id", 0);
		b.click()
	} else {
		a.click()
	}
}

function InitRecommendList() {
	var b = RecommendListLi(0);
	var a = RemmendList.Items.length;
	pageCount = Math.ceil(a / PageSize);
	pagination.pagination(a, {
		callback: RecommendListLi,
		num_display_entries: 0,
		items_per_page: PageSize,
		prev_text: " ",
		next_text: " "
	});
	$("#pagination").find(".prev").attr("class", "prev track").attr("name", "my-recommend-previous page");
	$("#pagination").find(".next").attr("class", "next track").attr("name", "my-recommend-next page");
	pageInfo.children("span").text("/" + pageCount);
	pageInfo.children("label").text(1);
	$(".tj1").hover(function() {
		if(timerId) {
			clearTimeout(timerId)
		}
	}, function() {
		setTimer(autoClick, TickSpace)
	});
	RecommendSuitList(b)
}

function GetRemmendListNew() {
	var a = "/Boot/_NewProduct";
	$.ajax({
		type: "GET",
		cache: false,
		dataType: "text",
		url: a,
		beforeSend: function() {},
		success: function(b) {
			if(b != "") {
				RecommendSuitList(b)
			}
		},
		error: function(b) {}
	})
}

function GetRemmendList(b, d, e, c, a) {
	if(a == "LookAndBuy") {
		GetProductByBrowseThenPurchase(b, d)
	} else {
		if(a == "LookAndLook") {
			GetProductByBrowseThenBrowse(b, d)
		} else {
			if(a == "BuyAndBuy") {
				GetProductByPurchaseThenPurchase(b, e, c, d)
			}
		}
	}
}

function RecommendSuitList(a) {
	if(window.location.href.toLowerCase().indexOf("order") >= 0) {
		return
	}
	var b = "/Boot/_Jifenhuangou";
	$.ajax({
		type: "GET",
		cache: false,
		data: "ram=" + Math.random(),
		dataType: "text",
		url: b,
		beforeSend: function() {},
		success: function(c) {
			if(c != "") {
				strResult = c;
				$("#recommendsuit").html(strResult)
			}
		}
	})
}

function getLen(a) {
	return a.replace(/[^\x00-\xff]/g, "xx").length
}
String.prototype.removeCharAt = function(a) {
	var b = this;
	if(a <= this.length) {
		if(a === 0) {
			b = this.substring(1)
		} else {
			if(a == this.length - 1) {
				b = this.substring(0, this.length - 1)
			} else {
				b = this.substring(0, a) + this.substring(a + 1)
			}
		}
	}
	return b
};
String.prototype.intercept = function(b) {
	var c = this;
	var a = c.getBytesLength();
	while(a > b) {
		c = c.removeCharAt(c.length - 1);
		a = c.getBytesLength()
	}
	return c
};
String.prototype.getBytesLength = function() {
	return this.length + (escape(this).split("%u").length - 1)
};